home *** CD-ROM | disk | FTP | other *** search
-
- (function(namespace, $)
- {
- namespace.PrefsManager = function()
- {
- this.service = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.translator.");
- this.service.QueryInterface(Components.interfaces.nsIPrefBranch2);
- this.service.addObserver('', this, false);
- };
-
- namespace.PrefsManager.prototype = {
- service: null,
-
- getPref: function(pref)
- {
- if(this.service == null) {
- throw new Exception('Service not initialized');
- }
-
- var prefType = this.getPrefType(pref);
-
- switch(prefType) {
- case this.service.PREF_BOOL:
- return this.service.getBoolPref(pref);
-
- case this.service.PREF_INT:
- return this.service.getIntPref(pref);
-
- case this.service.PREF_STRING:
- return this.service.getCharPref(pref);
- }
-
- return null;
- },
-
- getAllPrefs: function()
- {
- var prefs = {};
- var prefKeys = this.service.getChildList('', {});
-
- $.each(prefKeys, function(i, prefKey) {
- prefs[prefKey] = this.getPref(prefKey);
- }.bind(this));
-
- return prefs;
- },
-
- setPref: function(pref, value, type)
- {
- if(this.service == null) {
- throw new Exception('Service not initialized');
- }
-
- var prefType = type || this.getPrefType(pref);
-
- switch(prefType) {
- case this.service.PREF_BOOL:
- this.service.setBoolPref(pref, value);
- break;
-
- case this.service.PREF_INT:
- this.service.setIntPref(pref, value);
- break;
-
- case this.service.PREF_STRING:
- this.service.setCharPref(pref, value);
- break;
- }
- },
-
- getPrefType: function(pref)
- {
- return this.service.getPrefType(pref);
- },
-
- resetAllPrefs: function()
- {
- var preferences = this.getAllPrefs();
-
- $.each(preferences, function(k, v) {
- // continue if preference key starts from '_' which
- // means that it's internal preference and should not be defaulted
- if(k.substr(0, 1) == '_') return true;
-
- if(this.service.prefHasUserValue(k)) {
- this.service.clearUserPref(k);
- }
- }.bind(this));
- },
-
- observe: function(subject, topic, data)
- {
- // check if document exists because if prefs window
- // already closed it's throwing js error (see #44)
- if(topic != 'nsPref:changed' || !document) return;
-
- $(document).trigger('translatorPreferencesChanged.translator');
- },
- };
- })(com.igorgladkov.translator, translatorJQuery);